package main

import (
	"coze_for_sdk/config"
	"coze_for_sdk/middleware"
	"coze_for_sdk/model"
	"coze_for_sdk/router/api"
	"log"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	// 设置时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		log.Fatalf("设置时区失败: %v", err)
	}
	time.Local = loc

	// 初始化数据库
	dbConfig := model.Config{
		Host:         config.DefaultConfig.Database.Host,
		Port:         config.DefaultConfig.Database.Port,
		User:         config.DefaultConfig.Database.User,
		Password:     config.DefaultConfig.Database.Password,
		DBName:       config.DefaultConfig.Database.Name,
		MaxIdleConns: config.DefaultConfig.Database.MaxIdleConns,
		MaxOpenConns: config.DefaultConfig.Database.MaxOpenConns,
		MaxLifetime:  time.Duration(config.DefaultConfig.Database.MaxLifetime) * time.Second,
	}

	if err := model.Initialize(&dbConfig); err != nil {
		log.Fatalf("初始化数据库失败: %v", err)
	}

	r := gin.Default()

	r.Use(middleware.CorsMiddleware())

	api.AuthRoutes(r)

	// 启动服务器
	if err := r.Run(":" + config.DefaultConfig.Server.Port); err != nil {
		log.Fatalf("服务器启动失败: %v", err)
	}
}
